var request = require('../request');
var redis = require('../redis');
var readability = require('../co-read');
var $ = require('jquery')(require("jsdom").jsdom().parentWindow);
var xmlParser = require('xml2json');
var util = require('util');
var config = require('../../config/site');
var crypto = require('crypto');
var zlib = require('koa-zlib');

function Site() {}

Site.prototype.fetchData = function*() {
  console.log("fetching");
  var rss = {},
    siteList = config.site,
    itemList = new Array();
  var sthis = this;
  //循环每个订阅站点

  for (var i = siteList.length - 1; i >= 0; i--) {

    var itemResult =
      yield this.fetchListData(siteList[i].url, siteList[i].title);
    itemResult.forEach(function(page) {
      itemList.push(page);
    })

  };


  for (var i = 0, l = itemList.length; i < l; i++) {
    var item = itemList[i];
    console.log(i + "-----" + item.url);
    if (item == undefined) {
      continue;
    };
    if (item.url.indexOf("http") >= 0) {
      //content-cache
      var contentCacheId = util.format(config.contentCachePolicy.cacheId, item.hash);
      var content =
        yield redis.get(contentCacheId);
      if (!config.cacheEnable || !content) {
        //content-collect
        content =
          yield this.getContentData(item.url);
        if (content) {
          yield redis.set(contentCacheId, 1);
          yield redis.sadd("rss", item.title + "liuqq|" + content);
        }

      }

    }
  };
  console.log("end fetch");
};

Site.prototype.getData = function*() {
  var rss = {},
    siteList = config.site,
    itemList = new Array(),
    newItem = new Array();


  newItem = yield redis.srandmember("rss", config.limit);

  rss.title = config.title;
  rss.items = new Array();
  var tmpPage;
  for (var key in newItem) {
    tmpPage = newItem[key].split("liuqq|");
    rss.items.push({
      title: tmpPage[0],
      content: tmpPage[1]
    });
  }

  return rss;
};

Site.prototype.fetchListData = function*(url, titleRule) {
  var items = [];
  var response =
    yield request(url);
  if (response.error)
    throw new Error(response.error);
  if (response.statusCode != 200)
    throw new Error('response statusCode: ' + response.statusCode);

  var tmpUrl = "";
  var body = response.body;
  $(body).find(titleRule).each(function($this) {
    tmpUrl = $(this).attr('href')

    var md5 = crypto.createHash('md5');
    md5.update(tmpUrl);

    items.push({
      title: $(this).text(),
      url: tmpUrl,
      hash: md5.digest('hex')
    });
  });


  if (items.length === 0)
    throw new Error(sid + ': 无相关文章');

  return items;
}

/**
 * [*getContentData 通过read获取正文]
 * @param {[type]} conentUrl     [description]
 * @yield {[type]} [description]
 */
Site.prototype.getContentData = function*(contentUrl) {

  var result =
    yield readability(contentUrl);
  var dom = result.document;
  var html = "";
  var hTitle = html.title;
  var hContent = result.content;

  return hContent;
};


module.exports = Site;